package com.scsystem.xysc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.scsystem.xysc.aop.LogOperations;
import com.scsystem.xysc.mapper.ProductMapper;
import com.scsystem.xysc.pojo.Product;
import com.scsystem.xysc.pojo.vo.ReturnResult;
import com.scsystem.xysc.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.apache.ibatis.annotations.Update;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@Transactional
@CrossOrigin
@Tag(name = "商品接口")
@RequestMapping("/product")
public class ProductController {
    @Resource
    ProductMapper productMapper;
    @Resource
    private ProductService productService;

    @GetMapping("findname")
    @Operation(summary = "根据名字模糊查找商品")
    //@LogOperations(module = "商品模块",operation = "根据名字模糊查询商品")
    public List<Product> find(String name){

      //  ArrayList<Product> like = productMapper.findLike(name);
      QueryWrapper<Product> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("product_name",name).eq("product_status","已上架");
        List<Product> products = productMapper.selectList(queryWrapper);
        return products;

    }
    @GetMapping("findpage")
    @Operation(summary = "分页查询")
    public Page<Product> findPage(Integer yema,Integer count){
        QueryWrapper<Product> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("product_status","已上架");
        Page<Product> product = new Page<>(yema, count);
        Page<Product> productPage = productMapper.selectPage(product, queryWrapper);
        return productPage;
    }
    @PutMapping("update")
    @Operation(summary = "根据id修改商品信息")
    @LogOperations(module = "商品模块",operation = "根据商品id修改商品")
    public Boolean updateId(@RequestBody Product product){
        int i = productMapper.updateById(product);
        return i==1;
    }
    @PostMapping("insert")
    @Operation(summary = "添加商品信息，不用传时间过来.商品状态不传（已上架）就看不到")
    @LogOperations(module = "商品模块",operation = "添加商品")
    public Boolean insertProduct(@RequestBody Product product){
        product.setCreateTime(new Date());
        int insert = productMapper.insert(product);
        return insert==1;
    }
   @PutMapping("xiajia")
   @Operation(summary = "输入商品id下架商品")
   @LogOperations(module = "商品模块",operation = "下架商品")
   public Boolean goOut(Long l){
       int i = productMapper.goOutByid(l);
       return i==1;
   }
    @PutMapping("shangjia")
    @Operation(summary = "输入商品id上架商品")
    @LogOperations(module = "商品模块",operation = "上架商品")
    public Boolean goOutin(Long l){
        int i = productMapper.goByid(l);
        return i==1;
    }



   @DeleteMapping("delte")
    @Operation(summary = "根据id删除商品")
    @LogOperations(module = "商品模块",operation = "根据商品id删除商品")
    public Boolean DeleteBatchById(Long id){
        int i = productMapper.deleteById(id);
        return i==1;
    }
    @DeleteMapping("delte2")
    @Operation(summary = "根据id批量删除商品")
    @LogOperations(module = "商品模块",operation = "根据id批量删除商品")
    public Boolean DeleteBatchByIds(@RequestBody ArrayList<Long> list){
        int i = productMapper.deleteBatchIds(list);
        return i== list.size();
    }

    @GetMapping("purchase")
    @Operation(summary = "购买商品")
    @LogOperations(module = "商品模块",operation = "购买商品")
    @Parameters({
            @Parameter(name = "pid", description = "商品id"),
            @Parameter(name = "quantity", description = "购买数量"),
            @Parameter(name = "uid", description = "用户id")
    })
    public ReturnResult<Boolean> purchase(
            @RequestParam("pid") Long pid,
            @RequestParam("quantity") Integer quantity,
            @RequestParam("uid") Long uid){
        return productService.purchase(pid, quantity, uid);
    }



}
